package com.auxgroup.smarthome.inner.init;

import com.alibaba.druid.pool.DruidDataSource;
import com.auxgroup.smarthome.IserverInitService;
import com.auxgroup.smarthome.inner.service.BaseCodesDBService;
import com.auxgroup.smarthome.inner.vo.BaseCodesVo;
import com.auxgroup.smarthome.kit.SqlKit;
import com.auxgroup.smarthome.redis.config.ObjectRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by kevinchen on 2017/7/27.
 */
@Service
public class InnerInitService implements IserverInitService {
    @Autowired
    private ObjectRedis objectRedis;
    @Autowired
    private BaseCodesDBService baseCodesDBService;
    @Autowired
    private DruidDataSource dataSource;


    /**
     * 初始化执行
     * @param event
     */
    @Override
    public void appStartedOperate(ApplicationEvent event) {
        //执行sql脚本
        SqlKit.excuteSql(dataSource,"inner.sql");
        //把数据字典读到redis缓存
        List<BaseCodesVo> list = baseCodesDBService.findAll();
        for(BaseCodesVo baseCodesVo : list){
            String typeCode = baseCodesVo.getTypeCode();
            List<BaseCodesVo> listVo =  baseCodesDBService.findBaseCodes(typeCode);
            objectRedis.add(typeCode,1000L,listVo);
        }
    }

    @Override
    public void appClosedOperate(ApplicationEvent event) {
        System.err.println("inner-server微服务关闭");
    }
}
